package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.minio.MinIOService;
import com.heima.common.util.MediaThreadLocalUtil;
import com.heima.media.dto.WmMaterialDto;
import com.heima.media.entity.WmMaterial;
import com.heima.media.mapper.WmMaterialMapper;
import com.heima.media.service.IWmMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 自媒体图文素材信息表 服务实现类
 * </p>
 *
 * @author zsk
 * @since 2022-04-12
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements IWmMaterialService {


    @Autowired
    private MinIOService minIOService;

    /**
     * 图片上传
     * @param file
     * @return
     */
    @Override
    public ResponseResult<WmMaterial> upload(MultipartFile file) {

        //获取线程内user对象
        User user = MediaThreadLocalUtil.get();
        if (user == null){
            //返回提示，需要登陆后操作
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //调用MinIOService上传文件
        String url = minIOService.upload(file);

        //构建素材对象
        WmMaterial material = new WmMaterial();
        material.setUserId(user.getUserId());
        material.setUrl(url);
        material.setType(0);
        material.setIsCollection(false);
        material.setCreatedTime(new Date());
        //保存数据
        this.save(material);
        //返回前端数据
        return ResponseResult.okResult(material);
    }

    /**
     * 分页查询素材
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listByPage(WmMaterialDto dto) {

        //获取线程
        User user = MediaThreadLocalUtil.get();
        if (user == null){
            //返回提示
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //构建查询条件
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        if (dto.getIsCollection() != null){
            wrapper.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        }

        //构建分页条件
        Page<WmMaterial> page = new Page<>(dto.getPage(), dto.getSize());
        //只返回自己的素材
        wrapper.eq(WmMaterial::getUserId,user.getUserId());
        //按照添加图片时间降序
        wrapper.orderByDesc(WmMaterial::getCreatedTime);

        //查询
        Page<WmMaterial> iPage = this.page(page, wrapper);

        //构建返回条件
        PageResponseResult<List<WmMaterial>> result = new PageResponseResult<>(dto.getPage(),
                dto.getSize(), iPage.getTotal(), iPage.getRecords());

        //返回前端结果
        return result;
    }
}
